<?php
/*
 * File ookrijlessenmet.php for project Rijlesplanner.nl
 * Created on 11 mar 2008 at 14:36:23 by Ivar Pruijn
 */
class ookrijlessenmet extends modules {
	// Properties:
	protected $name = "ookrijlessenmet";
	protected $title = "Ook rijlessen met Rijlesplanner";
	protected $subtitle = "";
	protected $moduleOptions = array ();
	private $errors = array ();

	// Methods:

	/**
	 * void __construct ()
	 * This is the constructor method for this class.
	 */
	public function __construct() {
		parent :: __construct();
		$this->logger = parent :: getLogger();
		$this->user = parent :: getUser();
		$this->template = parent :: getTemplate();
	}

	/**
	 * Object process()
	 * 
	 * Processes the template and current module
	 * 
	 * @param Smarty template The Smarty template to process (sets variables etc.)
	 * @return Smarty The template that has been processed
	 */
	function process(& $template = null) {
		$this->template = $template;
		parent::loadModule("authentication",$template);
		
		if (!empty ($_POST)) {
			if (!$this->auth->accessTo($this->name,null,$_POST['action'])) {
				$this->actionAccessDenied();
			} else {
				switch ($_POST['action']) {
					case "ookrijlessenmet" :
						$this->ookrijlessenmetSubmit();
						$this->template->assign('moduleTemplate', 'modules/ookrijlessenmet/ookrijlessenmet_bezoeker.tpl');
						break;
	  	    		default :
	  	    			break;
				}
			}
		} else if(!empty($_GET['option'])) {
			if (!$this->auth->accessTo($this->name,$_GET['option'])) {
				$this->optionAccessDenied();
			} else {
	  	  		switch ($_GET['option']) {
	  	    		default:
						$this->ookrijlessenmetPageContent();
						$this->template->assign('moduleTemplate', 'modules/ookrijlessenmet/ookrijlessenmet_bezoeker.tpl');
	  	    			break;
	  	  		}
			}
		} else {
			parent::loadModule("reviews",$template);
			if($this->user instanceof Leerling) {
				$this->optionAccessDenied();
				$this->template->assign('moduleTemplate', 'common/option_accessdenied.tpl');
			} else if ($this->user instanceof Visitor) {
				$this->ookrijlessenmetPageContent();
				$this->template->assign('moduleTemplate', 'modules/ookrijlessenmet/ookrijlessenmet_bezoeker.tpl');
			}
		}
	}
	
	function ookrijlessenmetPageContent() {
		$errors = array ();
		$this->template->assign('errors', $errors);
	}

	function ookrijlessenmetSubmit() {
		$sendResult = false;
		$this->errors = array ();
		if (!isset ($_POST['your_first_name']) || empty ($_POST['your_first_name'])) { $this->addError("Geen voornaam ingevuld",75); } else { $your_first_name = $_POST['your_first_name']; }
		if (!isset ($_POST['your_last_name']) || empty ($_POST['your_last_name'])) { $this->addError("Geen achternaam ingevuld",76); } else { $your_last_name = $_POST['your_last_name']; }
		if (!isset ($_POST['straat']) || empty ($_POST['straat'])) { $this->addError("Geen straat ingevuld",77); } else { $straat = $_POST['straat']; }
		if (!isset ($_POST['huisnummer']) || empty ($_POST['huisnummer'])) { $this->addError("Geen huisnummer ingevuld",78); } else { $huisnummer = $_POST['huisnummer']; }
		if (!isset ($_POST['postcode']) || empty ($_POST['postcode'])) { $this->addError("Geen postcode ingevuld",79); } else { $postcode = $_POST['postcode']; }
		if (!isset ($_POST['woonplaats']) || empty ($_POST['woonplaats'])) { $this->addError("Geen woonplaats ingevuld",80); } else { $woonplaats = $_POST['woonplaats']; }
		if (!isset ($_POST['your_email']) || empty ($_POST['your_email'])) { 
			$this->addError("Geen e-mailadres ingevuld",81);  
		} else if (!validate_email($_POST['your_email'])) {
			$this->addError('Het opgegeven e-mailadres is niet correct',81 );
		}
		if (!isset ($_POST['telefoonnr']) || empty ($_POST['telefoonnr'])) { $this->addError("Geen telefoonnummer ingevuld",82); } else { $telefoonnr = $_POST['telefoonnr']; }
		if (!isset ($_POST['type_rijles']) || empty ($_POST['type_rijles'])) {
			$this->addError("Geen type rijles geselecteerd",74); 
		} else {
			switch($_POST['type_rijles']) {
				case "Categorie_A_(Motor)" :
			 		$type_rijles = "Categorie A (Motor)";
					break;
				case "Categorie_AM2_(brommer)" :
			 		$type_rijles = "Categorie AM2 (brommer)";
					break;
				case "Categorie_AM4_(brommobiel)" :
			 		$type_rijles = "Categorie AM4 (brommobiel)";
					break;
				case "Categorie_B_(auto)" :
			 		$type_rijles = "Categorie B (auto)";
					break;
				case "Categorie_C" :
			 		$type_rijles = "Categorie C";
					break;
				case "Categorie_D" :
			 		$type_rijles = "Categorie D";
					break;
				case "Categorie_E_bij_B" :
			 		$type_rijles = "Categorie E bij B";
					break;
				case "Categorie_E_bij_C" :
			 		$type_rijles = "Categorie E bij C";
					break;
				case "Categorie_E_bij_D" :
			 		$type_rijles = "Categorie E bij D";
					break;
				case "Categorie_F" :
			 		$type_rijles = "Categorie F";
					break;
				case "Categorie_Taxi" :
			 		$type_rijles = "Categorie Taxi";
					break;
				case "Overig" :
			 		$type_rijles = "Overig";
					break;
				default :
					$this->addError("Geen bekend type rijles ('".$_POST['type_rijles']."') geselecteerd",73); 
					break;
			}
		}
		if(count($this->errors) <= 0) {
			# 11-11-2008 -> Niek: Sending mail taken care off by new webservice call
			$this->logger->log("Going to sent ookrijlessenmet");
			try {
				$result = $this->wsclient->WS_Rijlesplanner_OokLessenMet(array (
					'FirstName'        => $_POST['your_first_name'],
	        'LastName'       => $_POST['your_last_name'],
	        'Straat'     => $_POST['straat'],
	        'Huisnummer'    => $_POST['huisnummer'],
	        'Postcode'    => $_POST['postcode'],
	        'Woonplaats'    => $_POST['woonplaats'],
	        'Emailadres'    => $_POST['your_email'],
	        'Telefoonnummer'    => $_POST['telefoonnr'],
	        'Rijles_cat'    => $_POST['type_rijles'],
	        'Opmerkingen' 	=> $_POST['your_comments']
				));
				$sendResult = $result->Success;
				
			} catch (SoapFault $e) {
				$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_DEBUG);
				$this->logger->log("WS Client".print_r($this->wsclient,true),PEAR_LOG_DEBUG);
				switch ($e->faultcode) {
					default :
						$errorcode = 61;
						$errormessage = 'Onbekende fout bij versturen van het formulier';
						break;
				}
				$this->addError($errormessage,$errorcode);
			}
		}
		$this->template->assign('sendResult', $sendResult);
		$this->template->assign('emailadres',$_POST['your_email']);
		$this->template->assign('voornaam',$_POST['your_first_name']);
		$this->template->assign('errors', $this->errors);
	}

	public function addError($message, $code) {
		$this->errors[] = array (
			'code' => $code,
			'message' => $message
		);
		$this->setSubtitle($message);
	}

	public function getTitle() {
		return $this->title;
	}

	public function getSubTitle() {
		return $this->subtitle;
	}

	public function setSubTitle($newVal = "") {
		if (!empty ($newVal)) {
			$this->subtitle = $newVal;
		}
	}

	/*
	 * string __toString()
	 * 
	 * Returns a String representation of this class.
	 * 
	 * @return string A representation of this class
	 */
	public function __toString() {
		return $this->title;
	}
}
?>
